package com.yunxi.service.esb.impl;

import com.yunxi.core.base.Constant;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.app.AppService;
import com.yunxi.eai.service.bom.BOMService;
import com.yunxi.eai.service.cms.CmsService;
import com.yunxi.eai.service.crm.CrmService;
import com.yunxi.eai.service.esb.ESBService;
import com.yunxi.eai.service.esb.OneToManyService;
import com.yunxi.eai.service.mes.MesService;
import com.yunxi.eai.service.pjbx.PjbxService;
import com.yunxi.eai.service.sap.SapService;
import com.yunxi.eai.service.tms.SFTmsService;
import com.yunxi.eai.service.tms.TmsService;
import com.yunxi.eai.service.tpf.TpfService;
import com.yunxi.eai.service.tsp.TspService;
import com.yunxi.eai.service.ups.UpsService;
import com.yunxi.eai.service.urm.UrmService;
import com.yunxi.eai.service.vdc.VDCService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * Created by Administrator on 2019/5/20.
 */
/*@Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class ESBServiceImpl implements ESBService {
    protected static final Logger LOGGER = LoggerFactory.getLogger(ESBServiceImpl.class);

    @Autowired
    @Qualifier("bomServiceImpl")
    BOMService bomService;

    @Autowired
    @Qualifier("mesServiceImpl")
    MesService mesService;

    @Autowired
    @Qualifier("sapServiceImpl")
    SapService sapService;

    @Autowired
    @Qualifier("oneToManyServiceImpl")
    OneToManyService oneToManyService;

    @Autowired
    @Qualifier("appServiceImpl")
    AppService appService;

    @Autowired
    @Qualifier("tspServiceImpl")
    TspService tspServiceImpl;

    @Autowired
    @Qualifier("crmServiceImpl")
    CrmService crmService;

    @Autowired
    @Qualifier("urmServiceImpl")
    UrmService urmService;

    @Autowired
    @Qualifier("tmsServiceImpl")
    TmsService tmsService;

    @Autowired
    @Qualifier("cmsServiceImpl")
    CmsService cmsService;

    @Autowired
    @Qualifier("vdcServiceImpl")
    VDCService vdcService;
    @Autowired
    @Qualifier("pjbxServiceImpl")
    PjbxService pjbxService;

    @Autowired
    @Qualifier("tpfServiceImpl")
    TpfService tpfService;

    @Autowired
    @Qualifier("upsServiceImpl")
    UpsService upsService;
    @Autowired
    @Qualifier("sftmsServiceImpl")
    SFTmsService sfTmsService;
    @Override
    public ApiResult distributeEsb(EsbVO esbVO) throws Exception {
        ApiResult apiResult = null;
        apiResult = checkEsb(esbVO);
        if (apiResult != null) {
            return apiResult;
        }
        String sourceSystem = esbVO.getSourceSystem();
        if (sourceSystem.equals(Constant.BOM)) {
            // Object apiResult=bomService.distributeBom(esbVO);
            // return apiResult;
        } else if (sourceSystem.equals(Constant.IAM)) {

        } else if (sourceSystem.equals(Constant.SAP)) {
            apiResult = sapService.distributeSap(esbVO);
            return apiResult;
        } else if (sourceSystem.equals(Constant.TMS)) {
            apiResult =tmsService.distributeTms(esbVO);
        } else if (sourceSystem.equals(Constant.AIEG)) {

        }else if (sourceSystem.equals(Constant.TSP)) {
            apiResult = tspServiceImpl.distributeTsp(esbVO);
            return apiResult;
        } else if (sourceSystem.equals(Constant.MES)) {
             apiResult = mesService.distributeMes(esbVO);
              return apiResult;
        } else if (sourceSystem.equals(Constant.CRM)) {
            apiResult = crmService.distributeCrm(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.PMS)) {

        } else if (sourceSystem.equals(Constant.VDC)) {
            apiResult =vdcService.distributeVDC(esbVO);
            return apiResult;
        } else if (sourceSystem.equals(Constant.ESB)) {
             apiResult = oneToManyService.distributeData(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.APP)){
            apiResult = appService.distributeApp(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.URM)){
            apiResult = urmService.distributeUrm(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.CMS)){
            apiResult = cmsService.distributeCms(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.YCYJ)){
            apiResult = tpfService.distributeTpf(esbVO);
            return apiResult;
        } else if (sourceSystem.equals(Constant.BXPT)){
            apiResult = pjbxService.distributePjbx(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.UPS)){
            apiResult = upsService.distributeUps(esbVO);
            return apiResult;
        }else if (sourceSystem.equals(Constant.SFTMS)){
            apiResult = sfTmsService.distributeSFTms(esbVO);
            return apiResult;
        }else {
            return ResponseUtil.fail(500, "客户端系统不匹配!");
        }
        return apiResult;
    }

    private ApiResult checkEsb(EsbVO esbVO) {
        if (esbVO == null) {
            return ResponseUtil.fail(500, "没有任何数据!");
        }
        String sourceSystem = esbVO.getSourceSystem();
        if (sourceSystem == null || sourceSystem.equals("")) {
            return ResponseUtil.fail(500, "客户端系统简称没有提供!");
        }
        String serviceName = esbVO.getServiceName();
        if (serviceName == null || serviceName.equals("")) {
            return ResponseUtil.fail(500, "业务接口没有提供!");
        }
        String requestId = esbVO.getRequestId();
        if (requestId == null || requestId.equals("")) {
            return ResponseUtil.fail(500, "消息流水号没有提供!");
        }
        String reqBody = esbVO.getReqBody();
        if (reqBody == null || reqBody.equals("")) {
            return ResponseUtil.fail(500, "请求参数没有提供!");
        }
        return null;
    }


}
